package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAccountTraceMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsAccountTrace;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsAccountTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/15 20:25
 */
@Service
public class DtsAccountTraceServiceImpl implements DtsAccountTraceService {
  @Autowired
    DtsAccountTraceMapper accountTraceMapper;
  @Autowired
    DtsUserMapper userMapper;
//    SELECT * from dts_account_trace WHERE user_id = (
//            SELECT id FROM dts_user WHERE user_level =2 AND deleted = 0 AND username like '%陈%')
    @Override
    public Map<String, Object> selectByPage(Integer page, Integer limit, String sort, String order, String username, String mobile, String[] statusArray) {
        IPage<DtsAccountTrace> iPage = new Page<>(page,limit);

        LambdaQueryWrapper<DtsAccountTrace> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            LambdaQueryWrapper<DtsUser> userWapper = new LambdaQueryWrapper<>();
            userWapper.like(DtsUser::getUsername,username);
            List<DtsUser> userList = userMapper.selectList(userWapper);
                Integer[] count = new Integer[userList.size()-1];
            for (int i = 0; i < userList.size(); i++) {
                LambdaQueryWrapper<DtsAccountTrace> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DtsAccountTrace::getUserId,userList.get(i).getId());
                List<DtsAccountTrace> list = accountTraceMapper.selectList(wrapper1);
                if (list!=null && list.size()>0){
                    for (DtsAccountTrace accountTrace : list) {
                        count[i] = accountTrace.getId();
                    }
                }
            }
            wrapper.in(DtsAccountTrace::getId,count);
        }
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc();
        }else {
            wrapper.orderByAsc();
        }
        if (!StringUtils.isEmpty(mobile)){
            wrapper.like(DtsAccountTrace::getMobile,mobile);
        }
        if (statusArray != null && statusArray.length > 0){
            wrapper.in(DtsAccountTrace::getStatus,statusArray);
        }
        iPage = accountTraceMapper.selectPage(iPage,wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("traceList",iPage.getRecords());

        return data;
    }

    @Override
    public void updateAccountTraceStatus(int id, int status, String traceMsg) {
        DtsAccountTrace accountTrace = accountTraceMapper.selectById(id);
        accountTrace.setStatus(status);
        if (!StringUtils.isEmpty(traceMsg)){
            accountTrace.setTraceMsg(traceMsg);
        }
        accountTrace.setUpdateTime(new Date());

        LambdaQueryWrapper<DtsAccountTrace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAccountTrace::getId,id);
        accountTraceMapper.update(accountTrace,wrapper);
    }
}
